package com.example.locationtracking.service.impl;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.example.locationtracking.model.POSTResponse;
import com.example.locationtracking.model.UserLocation;
import com.example.locationtracking.model.User;
import com.example.locationtracking.service.Service;

public class ServiceImpl implements Service {

	public POSTResponse register(User user) throws Exception {
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.set("Connection", "Close");
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
		HttpEntity<User> requestEntity = new HttpEntity<User>(user,requestHeaders);		
		RestTemplate restTemplate = RestTemplateService.getInstance().getRestTemplate();
		ResponseEntity<POSTResponse> responseEntity = null;
		String urlService = RestTemplateService.getInstance().getServiceConfig().getURLRegister();
		responseEntity = restTemplate.exchange(urlService,HttpMethod.POST, requestEntity, POSTResponse.class);
		return responseEntity.getBody();
	}

	@Override
	public boolean loginPinCode(String user, String pinCode) throws Exception {
		// TODO Auto-generated method stub
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
		HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
		
		RestTemplate restTemplate = RestTemplateService.getInstance().getRestTemplate();
		ResponseEntity<Boolean> responseEntity =null;		
		String url = RestTemplateService.getInstance().getServiceConfig().getURLLoginByPin();
		responseEntity = restTemplate.exchange(String.format(url, user,pinCode), HttpMethod.GET, requestEntity, Boolean.class);	
		return responseEntity.getBody();
	}

	@Override
	public List<User> getUserList() throws Exception {
		// TODO Auto-generated method stub
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json",Charset.forName("UTF-8"))));
		//requestHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
		HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
		
		RestTemplate restTemplate = RestTemplateService.getInstance().getRestTemplate();

		ResponseEntity<User[]> responseEntity =null;
		
		String urlService = RestTemplateService.getInstance().getServiceConfig().getURLGetUserList();
		responseEntity = restTemplate.exchange(urlService,HttpMethod.GET, requestEntity, User[].class);

		return Arrays.asList(responseEntity.getBody());
	}

	@Override
	public List<UserLocation> getListLocationByUser(User user) throws Exception {
		// TODO Auto-generated method stub
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json",Charset.forName("UTF-8"))));
		//requestHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
		HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
		
		RestTemplate restTemplate = RestTemplateService.getInstance().getRestTemplate();

		ResponseEntity<UserLocation[]> responseEntity =null;
		//String url = RestTemplateService.getInstance().getServiceConfig().getUrlListProperty();
		//String url = "http://rest-service.guides.spring.io/greeting";
		//String url = RestTemplateService.getInstance().getServiceConfig().getUrlProperty();
		//responseEntity = restTemplate.exchange(String.format(url), HttpMethod.GET, requestEntity, ListProperty.class);
		//responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Greeting.class);	
		String urlService = RestTemplateService.getInstance().getServiceConfig().getURLLocationUserList();
		responseEntity = restTemplate.exchange(String.format(urlService,user.getUserName()),
				HttpMethod.GET, requestEntity, UserLocation[].class);

		return Arrays.asList(responseEntity.getBody());
	}

	@Override
	public boolean addLocation(UserLocation location) throws Exception {
		// TODO Auto-generated method stub
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.set("Connection", "Close");
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
		HttpEntity<UserLocation> requestEntity = new HttpEntity<UserLocation>(location,requestHeaders);		
		RestTemplate restTemplate = RestTemplateService.getInstance().getRestTemplate();
		ResponseEntity<Boolean> responseEntity = null;
		String urlService = RestTemplateService.getInstance().getServiceConfig().getURLAddLocation();
		responseEntity = restTemplate.exchange(urlService,HttpMethod.POST, requestEntity, Boolean.class);
		return responseEntity.getBody();
	}

	@Override
	public List<UserLocation> getHistoryLocationUserList(String userName)
			throws Exception {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				HttpHeaders requestHeaders = new HttpHeaders();
				requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json",Charset.forName("UTF-8"))));
				//requestHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
				HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
				
				RestTemplate restTemplate = RestTemplateService.getInstance().getRestTemplate();

				ResponseEntity<UserLocation[]> responseEntity =null;
				//String url = RestTemplateService.getInstance().getServiceConfig().getUrlListProperty();
				//String url = "http://rest-service.guides.spring.io/greeting";
				//String url = RestTemplateService.getInstance().getServiceConfig().getUrlProperty();
				//responseEntity = restTemplate.exchange(String.format(url), HttpMethod.GET, requestEntity, ListProperty.class);
				//responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Greeting.class);	
				String urlService = RestTemplateService.getInstance().getServiceConfig().getURLHistoryLocationUserList();
				responseEntity = restTemplate.exchange(String.format(urlService,userName),
						HttpMethod.GET, requestEntity, UserLocation[].class);

				return Arrays.asList(responseEntity.getBody());
	}

	@Override
	public UserLocation getLastLocation(User user) throws Exception {
		// TODO Auto-generated method stub
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json",Charset.forName("UTF-8"))));
		//requestHeaders.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
		HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
		
		RestTemplate restTemplate = RestTemplateService.getInstance().getRestTemplate();

		ResponseEntity<UserLocation> responseEntity =null;
		//String url = RestTemplateService.getInstance().getServiceConfig().getUrlListProperty();
		//String url = "http://rest-service.guides.spring.io/greeting";
		//String url = RestTemplateService.getInstance().getServiceConfig().getUrlProperty();
		//responseEntity = restTemplate.exchange(String.format(url), HttpMethod.GET, requestEntity, ListProperty.class);
		//responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Greeting.class);	
		String urlService = RestTemplateService.getInstance().getServiceConfig().getURLLastLocation();
		responseEntity = restTemplate.exchange(String.format(urlService,user.getUserName()),
				HttpMethod.GET, requestEntity, UserLocation.class);
		return responseEntity.getBody();
	}

	@Override
	public boolean login(String user, String password) throws Exception {
		// TODO Auto-generated method stub
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
		HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
		
		RestTemplate restTemplate = RestTemplateService.getInstance().getRestTemplate();
		ResponseEntity<Boolean> responseEntity =null;		
		String url = RestTemplateService.getInstance().getServiceConfig().getURLLogin();
		responseEntity = restTemplate.exchange(String.format(url, user,password), HttpMethod.GET, requestEntity, Boolean.class);	
		return responseEntity.getBody();
	}

	
}
